home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Japanese / cpsimage.cab / data / sys / stdio.elf < prev    next >
Text File  |  2009-04-23  |  17KB  |  386 lines

  1. /*
  2. ** $Id: stdio.elf,v 1.18 2009/02/10 10:41:45 campanel Exp $
  3. */
  4.  
  5. #load "sys/lang.elf";
  6.  
  7.  
  8. /******************************************************************************/
  9. /*
  10. ** This class provides access to a files and directories on the system.
  11. */
  12. /* @createTempDirectory Returns a new unique temp directory FILE object. */
  13. /* @createTempFile Returns a new unique temp FILE object. */
  14. /* @deleteFile Deletes the file or directory. The directory must be empty. */
  15. /* @deleteFiles Deletes the directory and all of its contents. Will recursively delete subdirectoies
  16.    if the recurse option is true. Will act like deleteFile on non-directories. */
  17. /* @equals Does the other FILE object refer to the same file or directory. */
  18. /* @exists Does the file or directory exist. */
  19. /* @getAbsoluteFile Returns a FILE object with the absolute, non relative, path. */
  20. /* @getAbsolutePath Returns a STRING with the absolute, non relative, path. */
  21. /* @getCanonicalFile Returns a FILE object with the canonical path. The path is absolute and does not traverse symbolic links. */
  22. /* @getCanonicalPath Returns a STRING with the canonical path. The path is absolute and does not traverse symbolic links. */
  23. /* @getExt Returns a STRING with the filename extension. */
  24. /* @getNameNoExt Returns a STRING with the filename component with no extension. */
  25. /* @getName Returns a STRING with the filename component, including the extension. */
  26. /* @getParentFile Returns a FILE object with the parent path. May start using ".." if this is a relative path. */
  27. /* @getParentPath Returns a STRING with the parent path. May start using ".." if this is a relative path. */
  28. /* @getPath Returns a STRING with the full path, including the filename and extension. */
  29. /* @isAbsolute Is the path absolute. */
  30. /* @isDirectory Is the item a directory. */
  31. /* @isFile Is the item a file. */
  32. /* @lastModified Last modified time in milliseconds from 1/1/1970. */
  33. /* @listFiles Returns LIST of FILE object(s) from this directory. */
  34. /* @list Returns an LIST of STRING filenames from this directory. */
  35. /* @getSize Returns a LONG of the file size. Not valid for contents below a directory. */
  36. /* @listRoots Returns LIST of FILE objects with the toplevel filesystem roots. These are drives on Windows and "/" elsewhere. */
  37. /* @mkdir Creates the directory. */
  38. /* @mkdirs Creates the directory and all needed parents. */
  39. /* @toURL Creates URL object */
  40. /* @New
  41. // DESCRIPTION
  42.   Creates a new instance of FILE from a parent FILE and/or a path STRING. If
  43. neither is passed uses the current directory.
  44.  
  45. // ARGUMENTS
  46.   STRING  path    Path can include environment variables
  47.   FILE    parent  Parent file object.  Actual path is composed from both
  48.                   parent and path if they are both given.
  49.  
  50. // EXAMPLE's
  51.   #load "sys/stdlib.elf";
  52.   
  53.   FILE f1 = new (FILE, path: "$HOME");
  54.   LIST l1 = f1.list();   // Returns a LIST of files in $HOME
  55.   
  56.   FILE f2 = new (FILE, parent:f1, path:"bin");
  57.   LIST l2 = f2.list();   // Returns a LIST of files in $HOME/bin
  58.   
  59.   FILE f3 = new (FILE, parent:f1, path:".cshrc");
  60.   print f3.exists(); // Tests if file $HOME/.cshrc exists
  61.   
  62.   FILE f = new (FILE, parent: ".");
  63.   print f.list();   // List the contents of the current directory 
  64. */
  65. /* @renameFile Renames/moves the file or directory to the dest FILE object. */
  66. /* @copyFile Copies the file to the dest FILE object. Only works on files. */
  67. /* @sep Returns a STRING with the path separator. */
  68. /* @copyDirectory Copies the contents of a directory to a destination directory. New directories will be created as needed. Files will always be overwritten. */
  69. /******************************************************************************/
  70. CLASS FILE {
  71.     METHOD New( FILE parent, STRING path ) NATIVE "ElfFileMethods@builtins";
  72.     METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "ElfFileMethods@builtins";
  73.     METHOD Free() NATIVE "ElfFileMethods@builtins";
  74.  
  75.     /* Static Methods */
  76.     METHOD sep() RETURNS( STRING term ) NATIVE "ElfFileMethods@builtins";
  77.     METHOD listRoots() RETURNS( LIST files ) NATIVE "ElfFileMethods@builtins";
  78.     METHOD createTempFile( STRING name ) RETURNS( FILE file ) NATIVE "ElfFileMethods@builtins";
  79.     METHOD createTempDirectory( STRING name ) RETURNS( FILE file ) NATIVE "ElfFileMethods@builtins";
  80.  
  81.     /* Methods */
  82.     METHOD deleteFile() RETURNS( BOOLEAN success ) NATIVE "ElfFileMethods@builtins";
  83.     METHOD mkdir() RETURNS( BOOLEAN success ) NATIVE "ElfFileMethods@builtins";
  84.     METHOD mkdirs() RETURNS( BOOLEAN success ) NATIVE "ElfFileMethods@builtins";
  85.     METHOD renameFile( FILE dest ) RETURNS( BOOLEAN success ) NATIVE "ElfFileMethods@builtins";
  86.     METHOD copyFile( FILE dest, BOOLEAN overwrite ) RETURNS( BOOLEAN success ) NATIVE "ElfFileMethods@builtins";
  87.  
  88.     METHOD copyDirectory ( FILE dest, BOOLEAN recurse )
  89.         RETURNS ( BOOLEAN success )
  90.     {
  91.         success = FALSE;
  92.         if (!dest) return;
  93.  
  94.         // Create destination directory as needed
  95.         BOOLEAN destExist = dest.exists();
  96.         if (destExist == FALSE)
  97.             destExist = dest.mkdirs();
  98.         if (destExist == FALSE) return;
  99.  
  100.         // Get a list of FILE objects from the source and copy it
  101.         LIST allFiles = this.listFiles();
  102.         if (!allFiles || allFiles.length() == 0) {
  103.             success = TRUE;
  104.             return;
  105.         }
  106.  
  107.         FILE subDest;
  108.         foreach (allFiles : curFile) {
  109.             subDest = new (FILE, parent: dest, path: curFile.getName() );
  110.  
  111.             if ((curFile.isDirectory() == TRUE) && (recurse == TRUE)) {
  112.                 success = curFile.copyDirectory(dest:subDest, recurse:recurse);
  113.             }
  114.             if (curFile.isFile() == TRUE) {
  115.                 success = curFile.copyFile(overwrite:TRUE, dest:subDest);
  116.             }
  117.             if (success == FALSE) return;
  118.         }
  119.  
  120.         success = TRUE;
  121.     }
  122.  
  123.     METHOD deleteFiles( BOOLEAN recurse ) RETURNS( BOOLEAN success )
  124.     {
  125.         LIST list = this.listFiles();
  126.         INTEGER i;
  127.         FILE f;
  128.  
  129.         for( i = 0; i < list.length(); i++ ) {
  130.             f = list[i];
  131.  
  132.             if( f.isDirectory() && recurse ) {
  133.                 f.deleteFiles( recurse:recurse );
  134.             } else {
  135.                 f.deleteFile();
  136.             }
  137.         }
  138.         success = this.deleteFile();
  139.     }
  140.  
  141.     METHOD equals( FILE other ) RETURNS( BOOLEAN eqls ) NATIVE "ElfFileMethods@builtins";
  142.     METHOD exists() RETURNS( BOOLEAN exists ) NATIVE "ElfFileMethods@builtins";
  143.     METHOD isDirectory() RETURNS( BOOLEAN isdir ) NATIVE "ElfFileMethods@builtins";
  144.     METHOD isFile() RETURNS( BOOLEAN isfile ) NATIVE "ElfFileMethods@builtins";
  145.     METHOD isAbsolute() RETURNS( BOOLEAN isabs ) NATIVE "ElfFileMethods@builtins";
  146.     METHOD lastModified() RETURNS( LONG time ) NATIVE "ElfFileMethods@builtins";
  147.  
  148.     METHOD list() RETURNS( LIST files ) NATIVE "ElfFileMethods@builtins";
  149.     METHOD listFiles() RETURNS( LIST files ) NATIVE "ElfFileMethods@builtins";
  150.  
  151.     METHOD getAbsolutePath() RETURNS( STRING path ) NATIVE "ElfFileMethods@builtins";
  152.     METHOD getAbsoluteFile() RETURNS( FILE path ) NATIVE "ElfFileMethods@builtins";
  153.  
  154.     METHOD getCanonicalPath() RETURNS( STRING path ) NATIVE "ElfFileMethods@builtins";
  155.     METHOD getCanonicalFile() RETURNS( FILE path ) NATIVE "ElfFileMethods@builtins";
  156.  
  157.     METHOD getParentPath() RETURNS( STRING parent ) NATIVE "ElfFileMethods@builtins";
  158.     METHOD getParentFile() RETURNS( FILE parent ) NATIVE "ElfFileMethods@builtins";
  159.  
  160.     METHOD getPath() RETURNS( STRING path ) NATIVE "ElfFileMethods@builtins";
  161.     METHOD getName() RETURNS( STRING name ) NATIVE "ElfFileMethods@builtins";
  162.     METHOD getNameNoExt() RETURNS( STRING name ) NATIVE "ElfFileMethods@builtins";
  163.     METHOD getExt() RETURNS( STRING name ) NATIVE "ElfFileMethods@builtins";
  164.  
  165.     METHOD getSize() RETURNS( LONG size ) NATIVE "ElfFileMethods@builtins";
  166.  
  167.     METHOD toURL() RETURNS( URL url ) NATIVE "ElfFileMethods@builtins";
  168.  
  169.     /* Private Fields */
  170.     VOID handle;
  171. }
  172.  
  173. /******************************************************************************/
  174. /*
  175. ** This class provides communication capabilities for URLs.
  176. */
  177. /* @New
  178. // DESCRIPTION
  179.   Creates a new URL handler for the given URL. This currently supports sending and receiving a file.
  180. Other actions may be added later.
  181.  
  182. // ARGUMENTS
  183.   URL      url      URL
  184.  
  185. // EXAMPLE's
  186.   #load "sys/stdlib.elf";
  187.  
  188.   URL host = new( URL, proto:"http", host:"xin.xeroxlabs.com", port:80 );
  189.  
  190.   URL file1 = new( URL, context:host, file:"/images/file1.png" );
  191.   URL file2 = new( URL, context:host, file:"/images/file2.png" );
  192.  
  193.   FILE tmp = FILE.createTempFile();
  194.  
  195.   file1.recvFile( file:tmp );    // download the file
  196.   file2.sendFile( file:tmp );    // upload the file
  197.  
  198.   tmp.deleteFile();              // delete the temporary local file
  199.  
  200. // NOTES
  201.   This should be thought of as an abstract class. To support a new protocol extend this class using
  202. the format of URLHANDLER_"protocol". The general send/recvFile on URL then can be used and this invoked
  203. automatically.
  204. */
  205. /* @recvFile Downloads from the URL into the given file. Not all protocols can do this. */
  206. /* @sendFile Uploads to the URL from the given file. Not all protocols can do this. */
  207. /******************************************************************************/
  208. CLASS URLHANDLER {
  209.     METHOD New( URL url ) NATIVE "ElfUrlHdrMethods@builtins";
  210.     METHOD Free() NATIVE "ElfUrlHdrMethods@builtins";
  211.  
  212.     /* Others may be added later, like send/recvString, a connect/close, etc. */
  213.     METHOD sendFile( FILE file ) RETURNS( BOOLEAN success ) NATIVE "ElfUrlHdrMethods@builtins";
  214.     METHOD recvFile( FILE file ) RETURNS( BOOLEAN success ) NATIVE "ElfUrlHdrMethods@builtins";
  215.  
  216.     URL url;
  217. }
  218.  
  219. /******************************************************************************/
  220. /*
  221. ** This class provides HTTP communication capabilities for URLs.
  222. */
  223. /* @New
  224. // DESCRIPTION
  225.   Creates a new HTTP URL handler for the given URL. This currently supports sending and receiving a file.
  226.   Other actions may be added.
  227.  
  228. // ARGUMENTS
  229.   URL      url      URL
  230.  
  231. // EXAMPLE's
  232.   #load "sys/stdlib.elf";
  233.  
  234.   URL host = new( URL, proto:"http", host:"xin.xeroxlabs.com", port:80 );
  235.  
  236.   URL file1 = new( URL, context:host, file:"/images/file1.png" );
  237.   URL file2 = new( URL, context:host, file:"/images/file2.png" );
  238.  
  239.   FILE tmp = FILE.createTempFile();
  240.  
  241.   file1.recvFile( file:tmp );    // download the file
  242.   file2.sendFile( file:tmp );    // upload the file
  243.  
  244.   tmp.deleteFile();              // delete the temporary local file
  245.  
  246. */
  247. /* @recvFile Downloads from the URL into the given file using http. */
  248. /* @sendFile Uploads to the URL from the given file using http. */
  249. /******************************************************************************/
  250. CLASS URLHANDLER_HTTP EXTENDS URLHANDLER {
  251.     METHOD New( URL url ) NATIVE "ElfUrlHdrMethods@builtins";
  252.     METHOD Free() NATIVE "ElfUrlHdrMethods@builtins";
  253.  
  254.     /* Others may be added later, like send/recvString, a connect/close, etc. */
  255.     METHOD sendFile( FILE file ) RETURNS( BOOLEAN success ) NATIVE "ElfUrlHdrMethods@builtins";
  256.     METHOD recvFile( FILE file ) RETURNS( BOOLEAN success ) NATIVE "ElfUrlHdrMethods@builtins";
  257. }
  258.  
  259. /******************************************************************************/
  260. /*
  261. ** This class provides access to URLs.
  262. */
  263. /* @New
  264. // DESCRIPTION
  265.   Creates a new URL from the separate parts or from another URL.
  266.  
  267. // ARGUMENTS
  268.   URL      context  Resolve a relative URL against this URL.
  269.   STRING   spec     A free form URL. May be a relative path, if so the parent "context" should be given also.
  270.   STRING   proto    The protocol. Defaults to none.
  271.   STRING   host     The hostname. Defaults to none.
  272.   INTEGER  port     The network port number. Defaults to none.
  273.   STRING   file     The file path. Defaults to none.
  274.  
  275. // EXAMPLE's
  276.   #load "sys/stdlib.elf";
  277.  
  278.   URL host = new( URL, proto:"http", host:"xin.xeroxlabs.com", port:80 );
  279.  
  280.   URL file1 = new( URL, context:host, file:"/images/file1.png" );
  281.   URL file2 = new( URL, context:host, file:"/images/file2.png" );
  282.  
  283.   FILE tmp = FILE.createTempFile();
  284.  
  285.   file1.recvFile( file:tmp );    // download the file
  286.   file2.sendFile( file:tmp );    // upload the file
  287.  
  288.   tmp.deleteFile();              // delete the temporary local file
  289.  
  290. // NOTES
  291.   The sendFile, recvFile, and open methods use reflection to find a class with the name URLHANDLER_"protocol". If this class
  292.   exists it is used to handle the actual work.
  293. */
  294. /* @equals Does the other URL object refer to the same location. */
  295. /* @getHost Returns the host portion of the URL if there is one. */
  296. /* @getPort Returns the port portion of the URL if there is one, -1 otherwise. */
  297. /* @getFile Returns the file portion of the URL if there is one. */
  298. /* @getPath Returns the path portion of the URL if there is one. */
  299. /* @getProtocol Returns the protocol portion of the URL if there is one. */
  300. /* @getQuery Returns the query portion of the URL if there is one. */
  301. /* @getReference Returns the reference portion of the URL if there is one. */
  302. /* @getUserInfo Returns the user info portion of the URL if there is one. */
  303. /* @getAuthority Returns the authority portion of the URL if there is one. */
  304. /* @getHandlerName Returns the formated class name of the URLHANDLER for the current protocol. */
  305. /* @recvFile Downloads from the URL into the given file. Not all protocols can do this. */
  306. /* @sendFile Uploads to the URL from the given file. Not all protocols can do this. */
  307. /* @open Opens a handler to the given URL. Not all protocols can do this. */
  308. /******************************************************************************/
  309. CLASS URL {
  310.     METHOD New( URL context, STRING spec, STRING proto, STRING host, INTEGER port, STRING file ) NATIVE "ElfUrlMethods@builtins";
  311.     METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "ElfUrlMethods@builtins";
  312.     METHOD Free() NATIVE "ElfUrlMethods@builtins";
  313.  
  314.     METHOD equals( URL other ) RETURNS( BOOLEAN eqls ) NATIVE "ElfUrlMethods@builtins";
  315.  
  316.     METHOD getHost() RETURNS( STRING host ) NATIVE "ElfUrlMethods@builtins";
  317.     METHOD getPort() RETURNS( INTEGER port ) NATIVE "ElfUrlMethods@builtins";
  318.     METHOD getFile() RETURNS( STRING file ) NATIVE "ElfUrlMethods@builtins";
  319.     METHOD getPath() RETURNS( STRING path ) NATIVE "ElfUrlMethods@builtins";
  320.     METHOD getProtocol() RETURNS( STRING proto ) NATIVE "ElfUrlMethods@builtins";
  321.     METHOD getQuery() RETURNS( STRING query ) NATIVE "ElfUrlMethods@builtins";
  322.     METHOD getReference() RETURNS( STRING ref ) NATIVE "ElfUrlMethods@builtins";
  323.     METHOD getUserInfo() RETURNS( STRING user ) NATIVE "ElfUrlMethods@builtins";
  324.     METHOD getAuthority() RETURNS( STRING auth ) NATIVE "ElfUrlMethods@builtins";
  325.  
  326.     METHOD getHandlerName() RETURNS( STRING name )
  327.     {
  328.         name = "URLHANDLER_" + this.getProtocol().toUpper();
  329.     }
  330.  
  331.     METHOD recvFile( FILE file ) RETURNS( BOOLEAN success )
  332.     {
  333.         CLASSREF cr = REFLECTION.findClass( name:this.getHandlerName() );
  334.  
  335.         if( cr ) {
  336.             LIST params = ( url:this );
  337.             OBJECT obj = cr.newInstance( params:params );
  338.             LIST results;
  339.  
  340.             params = ( file:file );
  341.             cr.invokeMethod( obj:obj, name:"recvFile", params:params ) Returns( results:results );
  342.             success = results.ref( name:"success" );
  343.         } else {
  344.             success = FALSE;
  345.         }
  346.     }
  347.  
  348.     METHOD sendFile( FILE file ) RETURNS( BOOLEAN success )
  349.     {
  350.         CLASSREF cr = REFLECTION.findClass( name:this.getHandlerName() );
  351.  
  352.         if( cr ) {
  353.             LIST params = ( url:this );
  354.             OBJECT obj = cr.newInstance( params:params );
  355.             LIST results;
  356.  
  357.             params = ( file:file );
  358.             cr.invokeMethod( obj:obj, name:"sendFile", params:params ) Returns( results:results );
  359.             success = results.ref( name:"success" );
  360.         } else {
  361.             success = FALSE;
  362.         }
  363.     }
  364.  
  365.     METHOD open() RETURNS( URLHANDLER handler )
  366.     {
  367.         CLASSREF cr = REFLECTION.findClass( name:this.getHandlerName() );
  368.  
  369.         if( cr ) {
  370.             LIST params = ( url:this );
  371.             handler = cr.newInstance( params:params );
  372.         }
  373.     }
  374.  
  375.     METHOD toString() RETURNS( STRING str )
  376.     {
  377.         STRING pt = this.getProtocol();
  378.         STRING at = this.getAuthority();
  379.         STRING fl = this.getFile();
  380.         str = pt + "://" + at + fl;
  381.     }
  382.  
  383.     /* Private Fields */
  384.     VOID handle;
  385. }
  386.